/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import java.text.ParseException;
import java.util.Collection;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.persistence.EntityManager;
import converter.ItemFeedsConverter;
import converter.ItemFeedConverter;
import com.sun.jersey.api.core.ResourceContext;
import websrv.ItemFeed;

/**
 *
 * @author sexta13
 */
@Path("/itemFeeds/")
public class ItemFeedsResource {
    @Context
    protected ResourceContext resourceContext;
    @Context
    protected UriInfo uriInfo;
    

    /** Creates a new instance of ItemFeedsResource */
    public ItemFeedsResource() {
    }

    /**
     * Get method for retrieving a collection of ItemFeed instance in XML format.
     *
     * @return an instance of ItemFeedsConverter
     */
    @GET
    @Produces({"application/xml", "application/json"})
    public ItemFeedsConverter get(@QueryParam("start")
            @DefaultValue("0") int start, @QueryParam("max")
            @DefaultValue("10") int max, @QueryParam("expandLevel")
            @DefaultValue("1") int expandLevel, @QueryParam("query")
            @DefaultValue("SELECT e FROM ItemFeed e") String query) {
        PersistenceService persistenceSvc = PersistenceService.getInstance();
        try {
            persistenceSvc.beginTx();
            return new ItemFeedsConverter(getEntities(start, max, query), uriInfo.getAbsolutePath(), expandLevel);
        } finally {
            persistenceSvc.commitTx();
            persistenceSvc.close();
        }
    }

    /**
     * Post method for creating an instance of ItemFeed using XML as the input format.
     *
     * @param data an ItemFeedConverter entity that is deserialized from an XML stream
     * @return an instance of ItemFeedConverter
     */
    @POST
    @Consumes({"application/xml", "application/json"})
    public Response post(ItemFeedConverter data) {
        PersistenceService persistenceSvc = PersistenceService.getInstance();
        try {
            persistenceSvc.beginTx();
            EntityManager em = persistenceSvc.getEntityManager();
            ItemFeed entity = data.resolveEntity(em);
            createEntity(data.resolveEntity(em));
            persistenceSvc.commitTx();
            return Response.created(uriInfo.getAbsolutePath().resolve(entity.getId() + "/")).build();
        } finally {
            persistenceSvc.close();
        }
    }

    /**
     * Returns a dynamic instance of ItemFeedResource used for entity navigation.
     *
     * @return an instance of ItemFeedResource
     */
    @Path("{id}/")
    public ItemFeedResource getItemFeedResource(@PathParam("id") Integer id) {
        ItemFeedResource itemFeedResource = resourceContext.getResource(ItemFeedResource.class);
        itemFeedResource.setId(id);
        return itemFeedResource;
    }

    /**
     * Returns all the entities associated with this resource.
     *
     * @return a collection of ItemFeed instances
     */
    protected Collection<ItemFeed> getEntities(int start, int max, String query) {
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        return em.createQuery(query).setFirstResult(start).setMaxResults(max).getResultList();
    }

    /**
     * Persist the given entity.
     *
     * @param entity the entity to persist
     */
    protected void createEntity(ItemFeed entity) {
        entity.setId(null);
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        em.persist(entity);
    }
    
     /**
     * Returns a dynamic instance of ItemFeedResource used for entity navigation.
     *
     * @return an instance of ItemFeedResource
     */
    @GET
    @Path("{dataInicio}/{dataFim}")    
    public String getItemFeedByDateResource(@PathParam("dataInicio") String dataInicio, @PathParam("dataFim") String dataFim) throws ParseException {
        ItemFeedResource itemFeedResource = resourceContext.getResource(ItemFeedResource.class);
       return itemFeedResource.getEntityByDates(dataInicio, dataFim).toString() ;
       
        //return itemFeedResource;
    }

   }
